package com.mls.web.controller.api;

import com.mls.common.config.RuoYiConfig;
import com.mls.common.config.ServerConfig;
import com.mls.common.core.domain.AjaxResult;
import com.mls.common.utils.file.FileUploadUtils;
import com.mls.education.domain.EduComment;
import com.mls.education.domain.EduUser;
import com.mls.education.service.IEduCommentService;
import com.mls.education.service.IEduUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("api/person")
@ResponseBody
public class PersonController {

    @Autowired
    private IEduCommentService eduContentService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private IEduUserService eduUserService;

    private static final String FILE_DELIMETER = ",";

    /**
     * 发表评论
     */
    @PostMapping("/comment")
    public AjaxResult makeComment(EduComment eduComment){
        return AjaxResult.success(eduContentService.insertEduComment(eduComment));
    }

    /**
     * 个人信息管理
     */
    @PostMapping("/information")
    public AjaxResult updateInformation(EduUser eduUser){
        return AjaxResult.success(eduUserService.updateEduUser(eduUser));
    }



    /**
     * 文件上传
     */
    @PostMapping("/photo")
    public AjaxResult uploadPhoto(MultipartFile file) throws Exception{
        try {
            //上传图片路径
            String filePath = RuoYiConfig.getUploadPath();
            //上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            return ajax;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }
}
